//
//  Sensor.m
//  SujetObserver
//
//  Created by Pierre DUCHENE on 05/01/10.
//  Copyright 2010 Pierre DUCHENE. All rights reserved.
//

#import "TemperatureSensor.h"
#import "ObserverProtocol.h"
#import "SensorData.h"


@implementation TemperatureSensor

#pragma mark -
#pragma mark Memory Management
- (id)init
{
	if (self = [super init]) {
		observers = [[NSMutableSet alloc] init];
		sensorData = [[SensorData alloc] init];
		spreadStrategy = nil;
	}
	return self;
}

- (void)dealloc
{
	[observers release];
	observers = nil;
	
	[sensorData release];
	sensorData = nil;
	
	[(id)spreadStrategy release];
	spreadStrategy = nil;
	
	[super dealloc];
}

#pragma mark -
#pragma mark Accessors
@synthesize spreadStrategy;

- (void)setSpreadStrategy:(id<SpreadProtocol>)newStrategy
{
	if (spreadStrategy != newStrategy) {
		[spreadStrategy release];
		spreadStrategy = [newStrategy retain];
		[spreadStrategy configureWithContext:self];
	}
}

#pragma mark -
#pragma mark SubjectProtocol Methods
- (void)attach:(id<ObserverProtocol>)anObserver{
	[observers addObject:anObserver];
}

- (void)detach:(id<ObserverProtocol>)anObserver{
	[observers removeObject:anObserver];
}

- (NSSet*)getAllObservers
{
	return [NSSet setWithSet:observers];
}

#pragma mark -
#pragma mark SensorProtocol Methods

- (SensorData*)sensorData
{
	return [spreadStrategy sensorData];
}

- (SensorData*)dataForContext
{
	return sensorData;
}

- (void)setSensorValue:(NSNumber*)newValue;
{
	[sensorData setData:[newValue intValue]];
	[spreadStrategy execute];
}

@end
